package net.xisberto.bustime;

import java.util.ArrayList;

import net.xisberto.bustime.model.Bus;
import net.xisberto.bustime.model.Region;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class AddBusActivity extends BusListActivity implements OnItemSelectedListener, OnClickListener {
	private int selected_region = 0;

	private void prepareSpinner(ArrayList<Region> region_list) {
		ArrayList<String> items = new ArrayList<String>();
		for (Region region : region_list) {
			items.add(region.getName());
		}
		Spinner spinner = (Spinner)findViewById(R.id.spinnerRegion);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(this);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			this.title.setText(R.string.title_add_bus);
		} catch (NullPointerException e) {
		}
		setContentView(R.layout.view_bus_search);
		
		list_id = R.id.list_search_result;
		bus_list = (ArrayList<Bus>) getLastNonConfigurationInstance();
		if (bus_list != null) {
			populateList();
		}
		
		db.openDataBase();
		ArrayList<Region> regions = db.listRegion("");
		prepareSpinner(regions);
		db.close();

		ListView list = ((ListView)findViewById(R.id.list_search_result));
		list.setOnItemClickListener(this);
		
		findViewById(R.id.btn_search).setOnClickListener(this);
		
		Spinner spinner = (Spinner)findViewById(R.id.spinnerRegion);
		try {
			if (savedInstanceState.containsKey("selected_region")) {
				selected_region = savedInstanceState.getInt("selected_region");
				spinner.setSelection(selected_region);
			}
		} catch (NullPointerException e) {
			spinner.setSelection(0);
		}

		this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	}

	@Override
	public Object onRetainNonConfigurationInstance() {
		return bus_list;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putInt("selected_region", selected_region);
		Spinner spinner = (Spinner)findViewById(R.id.spinnerRegion);
		
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
		if (parent.equals(findViewById(R.id.spinnerRegion))) {
			selected_region = position;
		}
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
	}

	@Override
	public void onClick(View v) {
		if (v.equals(findViewById(R.id.btn_search))) {
			EditText edit_search = (EditText) findViewById(R.id.text_bus_search);
			db.openDataBase();
			bus_list = db.listSource(edit_search.getText().toString(), selected_region);
			populateList();
			db.close();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
		Bus bus = bus_list.get(position);
		db.openDataBase();
		if (db.insertIntoPersonal(bus.getId()) > -1) {
			Toast.makeText(this, R.string.toast_added_personal, Toast.LENGTH_SHORT).show();
		}
		db.close();
	}
	
}
